using System;
using DevExpress.Xpo;
using SmartPeople.Framework.SecuritySpace;
using SmartPeople.Framework.SecuritySpace;

namespace SmartPeople.Framework.Security.Providers.XPO
{
    [Persistent("SecuritySecuredType")]
    internal class XPSecuritySecuredType : XPCustomObject, ISecuritySecuredType
    {
        public XPSecuritySecuredType(Session session) : base(session)
        {
            // Use this constructor when you want to create a new object.
            // Place here your initialization code.
        }

        #region Old Constructor

        //public XPSecuritySecuredType(string name)
        //{
        //    //gID = Guid.NewGuid();
        //    this.name = name;
        //} 

        #endregion

        public XPSecuritySecuredType()
        {
        }

        #region Fields

        [Key(AutoGenerate = true)]
        [Persistent("GID")]
        private Guid gID;

        [Indexed("securityApplicationID", Unique = true, Name = "IX_SSTypeNameSApplicationID")]
        [Persistent("Name")]
        private string name;

        [Persistent("Description")]
        private string description;

        [Persistent("BaseSecuredType")]
        private XPSecuritySecuredType baseSecuredType;


        //[Association("SecurityApplicationSecuritySecuredType")]
        //public XPSecurityApplication SecurityApplication;

        //not from interface
        [Persistent("SecurityApplicationID")]
        private Guid securityApplicationID;

        //This non persistent property is needed for query purposes for XPO.
        [NonPersistent]
        public Guid SecurityApplicationID
        {
            get { return securityApplicationID; }
            //set { securityApplicationID = value; }
            set
            {
                if (securityApplicationID != value)
                {
                    Guid OldValue = securityApplicationID;
                    securityApplicationID = value;
                    OnChanged("SecurityApplicationID", OldValue, value);
                }
            }
        }


        [NonPersistent]
        internal Guid SecurityApplication
        {
            get { return securityApplicationID; }
        }

        #endregion

        #region Properties

        [NonPersistent]
        public Guid GID
        {
            get { return gID; }
            set
            {
                if (gID != value)
                {
                    Guid OldValue = gID;
                    gID = value;
                    OnChanged("GID", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    string OldValue = name;
                    name = value;
                    OnChanged("Name", OldValue, value);
                }
            }
        }


        [NonPersistent]
        public string Description
        {
            get { return description; }
            set
            {
                if (description != value)
                {
                    string OldValue = description;
                    name = value;
                    OnChanged("Description", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public ISecuritySecuredType BaseSecuredType
        {
            get { return baseSecuredType; }
            set
            {
                if (baseSecuredType != value)
                {
                    XPSecuritySecuredType OldValue = baseSecuredType;
                    baseSecuredType = (XPSecuritySecuredType) value;
                    OnChanged("Description", OldValue, value);
                }
            }
        }

        #endregion

        [Aggregated, Association("SecuredTypeActions", UseAssociationNameAsIntermediateTableName = true)]
        internal XPCollection<XPSecuritySecuredTypeAction> actions
        {
            get { return GetCollection<XPSecuritySecuredTypeAction>("actions"); }
        }

        [Aggregated, Association("SecuredTypeStates", UseAssociationNameAsIntermediateTableName = true)]
        internal XPCollection<XPSecuritySecuredTypeState> states
        {
            get { return GetCollection<XPSecuritySecuredTypeState>("states"); }
        }

        #region ISecuritySecuredType Members

        [NonPersistent]
        private XPSecuritySecuredTypeStates securedTypeStates;

        [NonPersistent]
        public ISecuritySecuredTypeStates States
        {
            get
            {
                if (securedTypeStates == null)
                    securedTypeStates = new XPSecuritySecuredTypeStates(this);
                return securedTypeStates;
            }
        }

        [NonPersistent]
        private XPSecuritySecuredTypeActions securedTypeActions;

        [NonPersistent]
        public ISecuritySecuredTypeActions Actions
        {
            get
            {
                if (securedTypeActions == null)
                    securedTypeActions = new XPSecuritySecuredTypeActions(this);
                return securedTypeActions;
            }
        }

        #endregion
    }
}